package com.gxa.controller;

import com.gxa.pojo.Agreement;
import com.gxa.service.AgreementService;
import com.gxa.utils.ResponseData;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * (Agreement)表控制层
 *
 * @author makejava
 * @since 2022-09-01 12:37:46
 */
@Api(tags = "Agreement", value = "Agreement")
@RestController
@RequestMapping("agreement")
public class AgreementController {
    /**
     * 服务对象
     */
    @Resource
    private AgreementService agreementService;

    /**
     * 通过条件查询
     *
     * @param agreement 筛选条件
     * @return 查询结果
     */
    @ApiOperation(value = "条件查找数据并分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agreementId", value = "合同编号", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementStartTime", value = "签约时间", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementEndTime", value = "结束时间", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementTenantName", value = "租户名称", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementTenantType", value = "租户类型", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementAttribute", value = "合同属性", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementPeopleId", value = "签约人id", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementBank", value = "开户行", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementCardNum", value = "卡号", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementBankName", value = "开户名称", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementProof", value = "收款凭证", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementRoomId", value = "$column.comment", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementNum", value = "$column.comment", dataType = "string", paramType = "body"),
    })
    @PostMapping("/queryByPage")
    public ResponseData queryByPage(Agreement agreement, @ApiParam(value = "页码") int page,@ApiParam(value = "每页多少条") int size) {
        ResponseData data = new ResponseData();
        data.setData(this.agreementService.queryByPage(agreement, page, size));
        return data;
    }

//    /**
//     * 通过条件查询单条数据
//     *
//     * @param agreement 主键
//     * @return 单条数据
//     */
//    @ApiOperation(value = "条件查找数据")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "agreementId", value = "合同编号", dataType = "int", paramType = "body"),
//            @ApiImplicitParam(name = "agreementStartTime", value = "签约时间", dataType = "string", paramType = "body"),
//            @ApiImplicitParam(name = "agreementEndTime", value = "结束时间", dataType = "string", paramType = "body"),
//            @ApiImplicitParam(name = "agreementTenantName", value = "租户名称", dataType = "string", paramType = "body"),
//            @ApiImplicitParam(name = "agreementTenantType", value = "租户类型", dataType = "string", paramType = "body"),
//            @ApiImplicitParam(name = "agreementAttribute", value = "合同属性", dataType = "string", paramType = "body"),
//            @ApiImplicitParam(name = "agreementPeopleId", value = "签约人id", dataType = "int", paramType = "body"),
//            @ApiImplicitParam(name = "agreementBank", value = "开户行", dataType = "string", paramType = "body"),
//            @ApiImplicitParam(name = "agreementCardNum", value = "卡号", dataType = "string", paramType = "body"),
//            @ApiImplicitParam(name = "agreementBankName", value = "开户名称", dataType = "string", paramType = "body"),
//            @ApiImplicitParam(name = "agreementProof", value = "收款凭证", dataType = "string", paramType = "body"),
//            @ApiImplicitParam(name = "agreementRoomId", value = "$column.comment", dataType = "int", paramType = "body"),
//            @ApiImplicitParam(name = "agreementNum", value = "$column.comment", dataType = "string", paramType = "body"),
//    })
//    @PostMapping("/query")
//    public ResponseData query(Agreement agreement) {
//        ResponseData responseData = new ResponseData();
//        responseData.setData(this.agreementService.query(agreement));
//        return responseData;
//    }

    /**
     * 新增数据
     *
     * @param agreement 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agreementId", value = "合同编号", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementStartTime", value = "签约时间", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementEndTime", value = "结束时间", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementTenantName", value = "租户名称", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementTenantType", value = "租户类型", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementAttribute", value = "合同属性", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementPeopleId", value = "签约人id", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementBank", value = "开户行", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementCardNum", value = "卡号", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementBankName", value = "开户名称", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementProof", value = "收款凭证", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementRoomId", value = "$column.comment", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementNum", value = "$column.comment", dataType = "string", paramType = "body"),
    })
    @PostMapping("/add")
    public ResponseData add(@RequestBody Agreement agreement) {
        ResponseData responseData = new ResponseData();
        if (this.agreementService.insert(agreement) > 0) {
            responseData.setCode(200);
            responseData.setMsg("success");
        } else {
            responseData.setCode(404);
            responseData.setMsg("fail");
        }
        return responseData;
    }

    /**
     * 编辑数据
     *
     * @param agreement 实体
     * @return 编辑结果
     */
    @ApiOperation(value = "修改数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agreementId", value = "合同编号", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementStartTime", value = "签约时间", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementEndTime", value = "结束时间", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementTenantName", value = "租户名称", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementTenantType", value = "租户类型", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementAttribute", value = "合同属性", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementPeopleId", value = "签约人id", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementBank", value = "开户行", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementCardNum", value = "卡号", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementBankName", value = "开户名称", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementProof", value = "收款凭证", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementRoomId", value = "$column.comment", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementNum", value = "$column.comment", dataType = "string", paramType = "body"),
    })
    @PutMapping("/change")
    public ResponseData edit(@RequestBody Agreement agreement) {
        ResponseData responseData = new ResponseData();
        Agreement update = this.agreementService.update(agreement);
        if (update.equals(null)) {
            responseData.setCode(404);
            responseData.setMsg("fail");
        } else {
            responseData.setCode(200);
            responseData.setData(update);
        }
        return responseData;
    }

    /**
     * 删除数据
     *
     * @param agreement 主键
     * @return 删除是否成功
     */
    @ApiOperation(value = "根据条件删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agreementId", value = "合同编号", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementStartTime", value = "签约时间", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementEndTime", value = "结束时间", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementTenantName", value = "租户名称", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementTenantType", value = "租户类型", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementAttribute", value = "合同属性", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementPeopleId", value = "签约人id", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementBank", value = "开户行", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementCardNum", value = "卡号", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementBankName", value = "开户名称", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementProof", value = "收款凭证", dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "agreementRoomId", value = "$column.comment", dataType = "int", paramType = "body"),
            @ApiImplicitParam(name = "agreementNum", value = "$column.comment", dataType = "string", paramType = "body"),
    })
    @DeleteMapping("/delete")
    public ResponseData deleteById(@RequestBody Agreement agreement) {
        ResponseData responseData = new ResponseData();
        if (this.agreementService.deleteById(agreement)) {
            responseData.setMsg("success");
        } else {
            responseData.setCode(404);
            responseData.setMsg("fail");
        }
        return responseData;
    }

    @ApiOperation(value = "文件上传")
    @PostMapping("/fileupload")
    public void upload(@RequestParam("myfiles")@ApiParam(value = "文件") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        // 获取文件上传到具体文件夹的绝对路径
        String realpath = request.getSession().getServletContext().getRealPath("WEB-INF\\img");
        System.out.println(realpath);
        // 获取上传的文件名
        String fileName = multipartFile.getOriginalFilename();
        //为了确保上传文件的重名问题，对文件名进行处理
        fileName = UUID.randomUUID().toString() + "_" + fileName;
        System.out.println(fileName);
        // 根据路径构建文件对象
        // 在构建过程中一定要注意路径问题
        File uploadFile = new File(realpath, fileName);
        // 判断指定文件夹uploadfiles是否存在，不存在就创建
        if (!uploadFile.exists()) {
            uploadFile.mkdirs();
        }
        // 上传文件
        multipartFile.transferTo(uploadFile);
    }
}

